package com.ftg.learn.bus.controller;


import com.ftg.learn.bus.VO.MenuVo;
import com.ftg.learn.bus.entity.Menu;
import com.ftg.learn.bus.service.IMenuService;
import com.ftg.learn.util.ResEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author lalala
 * @since 2022-03-07
 */

@RestController
@RequestMapping("/bus/menu")
@Api(tags = "菜单管理功能列表")
public class MenuController {

    @Resource
    IMenuService iMenuService;

    ResEntity re = new ResEntity();


//    @GetMapping("/showMenuByRid/{rid}")
//    @ApiOperation(value = "查询某角色下的菜单")
//    public ResEntity showMenuByRid(@PathVariable Long rid){
//        return re.OK(iMenuService.showMenuForRoleId(rid));
//    }
    /**
     * 注意当前方法返回值
     * 用于生成角色授权时，所用到的菜单树
     * all : 全部菜单树
     * myself: 当前角色拥有的
     * @param rid 角色 id
     */
    @GetMapping("/findMenuTree")
    public String showTree( Long rid){
        System.out.println("++++++++++++++++++++++++"+iMenuService.findMenuTree(rid).toString());
        return iMenuService.findMenuTree(rid).toString();
    }

    @GetMapping("/showMenuByUid/{uid}")
    @ApiOperation(value = "查询某用户下的菜单")
    public ResEntity showMenuByUid(@PathVariable Long uid){
        return re.OK(iMenuService.showMenuForUserId(uid));
    }

    @GetMapping("/showAllMenus")
    @ApiOperation(value = "获取所有菜单信息")
    public ResEntity showAllMenus(){
        return new ResEntity(0,"菜单获取成功！",iMenuService.list());
    }

    @PostMapping("/saveOrUpdateMenusByRid")
    @ApiOperation(value = "修改保存该角色的菜单")
    public ResEntity saveOrUpdateMenusByRid(@RequestParam Long rid,@RequestParam List<Long> ids){
        return iMenuService.saveOrUpdateMenusByRid(rid,ids)?re.OK():re.Failure("操作失败，请重试!");
    }

    @PutMapping("/updateMenu")
    @ApiOperation(value = "修改菜单信息")
    public ResEntity updateMenu(@RequestParam Long mid,@RequestParam String title){
        return iMenuService.updateForName(mid,title)?re.OK():re.Failure("修改菜单失败，请重试!");
    }

    /**
     * 登录成功以后，授权，查看当前用户权限
     */
    @ApiOperation(value = "分级当前用户所有授权")
    @PostMapping("/queryMenu")
    public List<MenuVo> queryMenu() {
        // 有可能会有不存在的情况，session失效情况........
        return iMenuService.queryMenu();
    }



}
